# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               compiler_blacklist_versions 1.0
PortGroup               github 1.0

github.setup            openexr openexr 2.3.0 v
# please set revision in subports
categories              graphics
license                 BSD
maintainers             {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description             OpenEXR Graphics Library
long_description        OpenEXR is a high dynamic-range (HDR) image file format developed \
                        by Industrial Light & Magic for use in computer imaging applications.
homepage                https://www.openexr.com
platforms               darwin
github.tarball_from     releases

depends_build-append    port:cctools
configure.env-append    NM=${prefix}/bin/nm

# avoid dependency on port:grep
configure.env-append    GREP=/usr/bin/grep

compiler.cxx_standard   2014

# please remove when in a later MacPorts version
# see https://github.com/macports/macports-base/commit/cdfb534f91748d22a627a6336e536915b1a5297d
compiler.blacklist-append \
                        {clang < 602}

if {${subport} ne "ilmbase"} {
    depends_build-append \
                        port:pkgconfig
}

if {${subport} eq ${name}} {
    revision            2

    checksums           rmd160  3444fe1aa647be9ab2e6ea1638e27ea97c3f6688 \
                        sha256  fd6cb3a87f8c1a233be17b94c74799e6241d50fc5efd4df75c7a4b9cf4e25ea6 \
                        size    18412067

    depends_lib-append  port:ilmbase \
                        port:zlib

    # ImfCompressor.h and ImfFloatVectorAttribute.h are included in other header files
    # see #49965
    # see also https://lists.nongnu.org/archive/html/openexr-devel/2013-06/msg00001.html
    # see also https://gitweb.gentoo.org/repo/gentoo.git/tree/media-libs/openexr/files/openexr-2.2.0-Install-missing-header-files.patch?h=master
    patchfiles-append   patch-IlmImf-Makefile.diff

    # OS X < 10.6 lacks posix_memalign
    # see https://github.com/openexr/openexr/issues/59
    patchfiles-append   patch-IlmImf-ImfSystemSpecific.h.diff

    # which upgrading, do not find old header files and libraries
    # see #51470 for MacPorts report
    # see https://github.com/openexr/openexr/issues/130 for upstream report
    patchfiles-append   patch-search_order.diff
    configure.ldflags-delete \
                        -L${prefix}/lib
    # code generated with -Os on clang-3.9 crashes openexr in
    # #0  Imf_2_2::DwaCompressor::initializeFuncs () at ImfDwaCompressor.cpp:2865
    # -O, -O2, -O3 work, though
    if { ${configure.compiler} eq "macports-clang-3.9" } {
        configure.optflags "-O2"
    }
    # clang-4.0 and later are not yet tested, use -O2 just to be on the safe side
    if { ${configure.compiler} eq "macports-clang-4.0" || ${configure.compiler} eq "macports-clang-5.0" || ${configure.compiler} eq "macports-clang-6.0" || ${configure.compiler} eq "macports-clang-7.0" || ${configure.compiler} eq "macports-clang-8.0" || ${configure.compiler} eq "macports-clang-9.0"} {
        configure.optflags "-O2"
    }
}

subport ilmbase {
    revision    1

    distname    ilmbase-${version}
    checksums   rmd160  bd60ebfdc114e186db87f8c8b92c4f031b17e0d9 \
                sha256  456978d1a978a5f823c7c675f3f36b0ae14dba36638aeaa3c4b0e784f12a3862 \
                size    595490

    depends_build   port:gsed

    # Bitrotted ucontext usage is buggy, don't bother with it.
    patchfiles-append \
                patch-configure.diff
}

set python_versions {27 35 36 37 38}

foreach pver ${python_versions} {
    subport py${pver}-pyilmbase {
        PortGroup   active_variants 1.1

        revision    3
        distname    pyilmbase-${version}
        checksums   rmd160  127329978f2fd5793a267d1c7c46d5514d1d46b7 \
                    sha256  9c898bb16e7bc916c82bebdf32c343c0f2878fc3eacbafa49937e78f2079a425 \
                    size    524975

        # so long as boost can only be installed with one version of python at a time,
        #    conflict with all other pyilmbase ports
        foreach over ${python_versions} {
            if {${over} ne ${pver}} {
                conflicts-append py${over}-pyilmbase
            }
        }

        configure.args-append   \
            --with-boost-python-libname=boost_python${pver}-mt

        set pbranch [string range ${pver} 0 end-1].[string index ${pver} end]
        configure.python ${prefix}/bin/python${pbranch}

        depends_lib-append     \
            port:zlib          \
            port:ilmbase       \
            port:python${pver} \
            port:boost         \
            port:py${pver}-numpy

        require_active_variants boost python${pver}
    }
}

subport openexr_viewers {
    revision            1
    distname            openexr_viewers-${version}

    checksums           rmd160  cf8cebf8075959db7539cc1f2858b1177b9b8c3e \
                        sha256  9b8a6abebfda17586ef7e5fea9ba8e87a3cc688676681ef306b445c3f4034175 \
                        size    532154

    depends_lib-append  port:openexr                \
                        path:lib/libfltk.dylib:fltk \
                        port:ctlang

    # do not attempt to have fltk build application bundle
    patchfiles-append   patch-fltk.diff
}

# The bundled libtool doesn't support -stdlib=...
patchfiles-append       patch-ltmain.sh.diff

test.run                yes
test.target             check

livecheck.type          regex
livecheck.url           ${homepage}/downloads.html
livecheck.regex         ${name}-(\\d+(?:\\.\\d+)*)
